package com.uff.hmstpa.controller.impl;

import java.util.Map;

import org.joda.time.DateTime;

import com.uff.hmstpa.controller.ScheduleSystemController;
import com.uff.hmstpa.model.ScheduleSystemModel;
import com.uff.hmstpa.model.domain.Appointment;
import com.uff.hmstpa.util.exception.BusinessRuleException;
import com.uff.hmstpa.util.observer.Observer;
import com.uff.hmstpa.util.observer.Subject;
import com.uff.hmstpa.vision.ScheduleSystemView;

public class ScheduleSystemControllerImpl implements ScheduleSystemController {

	private static ScheduleSystemController SOLE_INSTANCE = new ScheduleSystemControllerImpl();

	private ScheduleSystemView view;
	private ScheduleSystemModel model;

	private ScheduleSystemControllerImpl() {
		init();
	}

	public static ScheduleSystemController instance() {
		return SOLE_INSTANCE;
	}

	private void init() {}

	public void setModel(ScheduleSystemModel model) {
		this.model = model;		
	}

	public void setView(ScheduleSystemView view) {
		this.view = view;
		if(model != null) {
			((Subject) model).registerObserver((Observer) view);
		}
	}

	public ScheduleSystemView getView() {
		return this.view;
	}

	public void selectDoctor(String doctorName) {
		model.getMediator().setSelectedDoctor(doctorName);
	}

	public void createAppointment(Map<String, Object> params) {
		String crm = (String) params.get("crm");
		String cpf = (String) params.get("cpf"); 
		String time = (String) params.get("time"); 
		boolean homeride = (Boolean) params.get("homeride");

		try {
			model.getMediator().createAppointment(crm, cpf, time, homeride);
		} catch (BusinessRuleException e) {
			e.printStackTrace();
		}
	}

	public void deleteAppointment(Appointment appointment) {
		String crm = appointment.getDoctor().getCRM();
		String cpf = appointment.getPatient().getCpf();
		DateTime time = appointment.getTime();

		try {
			model.getMediator().deleteAppointment(crm, cpf, time);
		} catch(BusinessRuleException e) {
			e.printStackTrace();
			//TODO informar view do erro
		}
	}

	public void editAppointment(Map<String, Object> params) {
		String crm = (String) params.get("crm");
		String cpf = (String) params.get("cpf");
		DateTime time = (DateTime) params.get("time");
		try {
			model.getMediator().editAppointment(crm, cpf, time);
		} catch (BusinessRuleException e) {
			// TODO informar view do erro
			e.printStackTrace();
		}		
	}

	public void createPatient(Map<String, Object> params){
		String cpf = (String) params.get("cpf");
		String name = (String) params.get("name");
		int healthplan = (Integer) params.get("healthplan");
		try {
			model.getMediator().createPatient(cpf, name, healthplan);
		} catch (BusinessRuleException e) {
			// TODO informar view do erro
			e.printStackTrace();
		}
	}

	public void deletePatient(Map<String, Object> params) {
		String cpf = (String) params.get("cpf");
		try {
			model.getMediator().deletePatient(cpf);
		} catch (BusinessRuleException e) {
			// informar view do erro
			e.printStackTrace();
		}
	}

	public void editPatient(Map<String, Object> params) {
		String cpf = (String) params.get("cpf");
		String name = (String) params.get("name");
		int healthplan = (Integer) params.get("healthplan");
		try {
			model.getMediator().editPatient(cpf, name, healthplan);
		} catch (BusinessRuleException e) {
			// TODO informar view do erro
			e.printStackTrace();
		}		
	}

}
